﻿using System;
using System.Collections.Generic;
using System.Drawing;
using TigerAndGoats.Sprites;

namespace TigerAndGoats.Framework
{
    public class GraphNode
    {
        public GraphNode(string ordinate, Action<GraphNode> initNodeHandler)
        {
            this._ordinate = ordinate;
            initNodeHandler?.Invoke(this);
        }

        public GraphNode(ChequerGraph entireGraph, string ordinate, Action<GraphNode> initNodeHandler)
        {
            initNodeHandler?.Invoke(this);
            this._ordinate = ordinate;
            entireGraph.graphNodes[ordinate] = this;
        }

        public static GraphNode Empty { get {
                return new GraphNode(string.Empty, null);
            }
        }

        public static bool operator== (GraphNode item1, GraphNode Item2)
        {
            return item1?._ordinate == Item2?._ordinate;
        }

        public static bool operator !=(GraphNode item1, GraphNode Item2)
        {
            return item1?._ordinate != Item2?._ordinate;
        }

        public GraphNode LeftTop { get; internal set; }
        public GraphNode Top { get; internal set; }
        public GraphNode RightTop { get; internal set; }
        public GraphNode MiddleLeft { get; internal set; }
        public GraphNode MiddleRight { get; internal set; }
        public GraphNode BottomLeft { get; internal set; }
        public GraphNode Bottom { get; internal set; }
        public GraphNode BottomRight { get; internal set; }

        public List<GraphNode> GetAllReachableNodes()
        {
            var result = new List<GraphNode>();

            if (LeftTop != GraphNode.Empty)
            {
                result.Add(LeftTop);
            }

            if (Top != GraphNode.Empty)
            {
                result.Add(Top);
            }

            if (RightTop != GraphNode.Empty)
            {
                result.Add(RightTop);
            }

            if (MiddleLeft != GraphNode.Empty)
            {
                result.Add(MiddleLeft);
            }

            if (MiddleRight != GraphNode.Empty)
            {
                result.Add(MiddleRight);
            }

            if (BottomLeft != GraphNode.Empty)
            {
                result.Add(BottomLeft);
            }

            if (Bottom != GraphNode.Empty)
            {
                result.Add(Bottom);
            }

            if (BottomRight != GraphNode.Empty)
            {
                result.Add(BottomRight);
            }

            return result;
        }

        private string _ordinate; // 一般是有值的，因为初始化的棋盘的时候已经赋值了
        private Piece _piece { get; set; } = Piece.Empty;

        public string GetOrdinate()
        {
            return _ordinate;
        }

        public Piece GetPiece()
        {
            return _piece;
        }

        public Piece SetPiece(Piece newPiece)
        {
            // 接受新位置
            newPiece.SetLocation(this);
            // 到达新位置
            _piece = newPiece;
            return _piece;
        }

        public void SetPieceEmpty()
        {
            _piece = Piece.Empty;
        }

        public void DrawPiece(System.Drawing.Graphics graphics, Piece piece)
        {
            if (piece != null)
            {
                _piece = piece;
                _piece.SetLocation(this);
                _piece.Draw(graphics);
            }
        }

        public void DrawPiece(System.Drawing.Graphics graphics)
        {
            if (_piece != Piece.Empty)
            {
                _piece.Draw(graphics);
            }
        }

        public void Initialize(Action<GraphNode> initNodeHandler)
        {
            initNodeHandler?.Invoke(this);
        }
    }
}